home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_include / SCSI / SG.H < prev   
C/C++ Source or Header  |  1999-09-17  |  11KB  |  225 lines

  1. #ifndef _SCSI_GENERIC_H
  2. #define _SCSI_GENERIC_H
  3.  
  4. /*
  5.    History:
  6.     Started: Aug 9 by Lawrence Foard (entropy@world.std.com), to allow user 
  7.      process control of SCSI devices. 
  8.     Development Sponsored by Killy Corp. NY NY
  9. Original driver (sg.h):
  10. *       Copyright (C) 1992 Lawrence Foard
  11. 2.x extensions to driver:
  12. *       Copyright (C) 1998, 1999 Douglas Gilbert
  13.  
  14.  
  15.     Version: 2.1.31 (990327)
  16.     This version for later 2.1.x series and 2.2.x kernels
  17.     D. P. Gilbert (dgilbert@interlog.com, dougg@triode.net.au)
  18.  
  19.     Changes since 2.1.30 (990320)
  20.         - memory tweaks: change flags on kmalloc (GFP_KERNEL to GFP_ATOMIC)
  21.         -                increase max allowable mid-level pool usage
  22.     Changes since 2.1.21 (990315)
  23.         - skipped to 2.1.30 indicating interface change (revert to 2.1.9)
  24.         - remove attempt to accomodate cdrecord 1.8, will fix app
  25.         - keep SG_?ET_RESERVED_SIZE naming for clarity
  26.     Changes since 2.1.20 (990313)
  27.         - ommission: left out logic for SG_?ET_ALT_INTERFACE, now added
  28.     Changes since 2.1.9 (990309)
  29.         - skipped to version 2.1.20 to indicate some interface changes
  30.         - incorporate sg changes to make cdrecord 1.8 work (had its
  31.           own patches that were different from the original)
  32.         - change SG_?ET_BUFF_SIZE to SG_?ET_RESERVED_SIZE for clarity
  33.     Changes since 2.1.8 (990303)
  34.         - debug ">9" option dumps debug for _all_ active sg devices
  35.         - increase allowable dma pool usage + increase minimum threshhold
  36.         - pad out sg_scsi_id structure
  37.     Changes since 2.1.7 (990227)
  38.         - command queuing now "non-default" [back. compat. with cdparanoia]
  39.         - Tighten access on some ioctls
  40.  
  41.  
  42.     New features and changes:
  43.         - per file descriptor (fd) write-read sequencing and command queues.
  44.         - command queuing supported (SG_MAX_QUEUE is maximum per fd).
  45.         - scatter-gather supported (allowing potentially megabyte transfers).
  46.         - the SCSI target, host and driver status are returned
  47.           in unused fields of sg_header (maintaining its original size).
  48.         - asynchronous notification support added (SIGPOLL, SIGIO) for
  49.           read()s ( write()s should never block).
  50.         - pack_id logic added so read() can be made to wait for a specific
  51.           pack_id. 
  52.         - uses memory > ISA_DMA_THRESHOLD if adapter allows it (e.g. a
  53.           pci scsi adapter).
  54.         - this driver no longer uses a single SG_BIG_BUFF sized buffer
  55.           obtained at driver/module init time. Rather it obtains a 
  56.           SG_SCATTER_SZ buffer when a fd is open()ed and frees it at
  57.           the corresponding release() (ie pr fd). Hence open() can return
  58.           ENOMEM! If write() request > SG_SCATTER_SZ bytes for data then
  59.           it can fail with ENOMEM as well (if so, scale back).
  60.         - adds several ioctl calls, see ioctl section below.
  61.         - SG_SCATTER_SZ's presence indicates this version of "sg" driver.
  62.  
  63.  Good documentation on the original "sg" device interface and usage can be
  64.  found in the Linux HOWTO document: "SCSI Programming HOWTO" by Heiko
  65.  Eissfeldt; last updated 7 May 1996. I will add more info on using the
  66.  extensions in this driver as required. A quick summary:
  67.  An SG device is accessed by writing SCSI commands plus any associated 
  68.  outgoing data to it; the resulting status codes and any incoming data
  69.  are then obtained by a read call. The device can be opened O_NONBLOCK
  70.  (non-blocking) and poll() used to monitor its progress. The device may be
  71.  opened O_EXCL which excludes other "sg" users from this device (but not 
  72.  "sd", "st" or "sr" users). The buffer given to the write() call is made
  73.  up as follows:
  74.         - struct sg_header image (see below)
  75.         - scsi command (6, 10 or 12 bytes long)
  76.         - data to be written to the device (if any)
  77.  
  78.  The buffer received from the corresponding read() call contains:
  79.         - struct sg_header image (check results + sense_buffer)
  80.         - data read back from device (if any)
  81.  
  82.  The given SCSI command has its LUN field overwritten internally by the
  83.  value associated with the device that has been opened.
  84.  
  85.  Memory (RAM) is used within this driver for direct memory access (DMA)
  86.  in transferring data to and from the SCSI device. The dreaded ENOMEM
  87.  seems to be more prevalent under early 2.2.x kernels than under the
  88.  2.0.x kernel series. For a given (large) transfer the memory obtained by
  89.  this driver must be contiguous or scatter-gather must be used (if
  90.  supported by the adapter). [Furthermore, ISA SCSI adapters can only use
  91.  memory below the 16MB level on a i386.]
  92.  This driver tries hard to find some suitable memory before admitting
  93.  defeat and returning ENOMEM. All is not lost if application writers
  94.  then back off the amount they are requesting. The value returned by
  95.  the SG_GET_RESERVED_SIZE ioctl is guaranteed to be available (one
  96.  per fd). This driver does the following:
  97.    -  attempts to reserve a SG_SCATTER_SZ sized buffer on open(). The
  98.       actual amount reserved is given by the SG_GET_RESERVED_SIZE ioctl().
  99.    -  each write() needs to reserve a DMA buffer of the size of the
  100.       data buffer indicated (excluding sg_header and command overhead).
  101.       This buffer, depending on its size, adapter type (ISA or not) and
  102.       the amount of memory available will be obtained from the kernel
  103.       directly (get_free_pages or kmalloc) or the from the scsi mid-level
  104.       dma pool (taking care not to exhaust it).
  105.       If the buffer requested is > SG_SCATTER_SZ or memory is tight then
  106.       scatter-gather will be used if supported by the adapter.
  107.   -   write() will also attempt to use the buffer reserved on open()
  108.       if it is large enough.
  109.  The above strategy ensures that a write() can always depend on a buffer 
  110.  of the size indicated by the SG_GET_RESERVED_SIZE ioctl() (which could be
  111.  0, but at least the app knows things are tight in advance).
  112.  Hence application writers can adopt quite aggressive strategies (e.g. 
  113.  requesting 512KB) and scale them back in the face of ENOMEM errors.
  114.  N.B. Queuing up commands also ties up kernel memory.
  115.  
  116.  More documentation can be found at www.netwinder.org/~dougg
  117. */
  118.  
  119. #define SG_MAX_SENSE 16   /* too little, unlikely to change in 2.2.x */
  120.  
  121. struct sg_header
  122. {
  123.     int pack_len;    /* [o] reply_len (ie useless), ignored as input */
  124.     int reply_len;   /* [i] max length of expected reply (inc. sg_header) */
  125.     int pack_id;     /* [io] id number of packet (use ints >= 0) */
  126.     int result;      /* [o] 0==ok, else (+ve) Unix errno code (e.g. EIO) */
  127.     unsigned int twelve_byte:1; 
  128.         /* [i] Force 12 byte command length for group 6 & 7 commands  */
  129.     unsigned int target_status:5;   /* [o] scsi status from target */
  130.     unsigned int host_status:8;     /* [o] host status (see "DID" codes) */
  131.     unsigned int driver_status:8;   /* [o] driver status+suggestion */
  132.     unsigned int other_flags:10;    /* unused */
  133.     unsigned char sense_buffer[SG_MAX_SENSE]; /* [o] Output in 3 cases:
  134.            when target_status is CHECK_CONDITION or 
  135.            when target_status is COMMAND_TERMINATED or
  136.            when (driver_status & DRIVER_SENSE) is true. */
  137. };      /* This structure is 36 bytes long on i386 */
  138.  
  139.  
  140. typedef struct sg_scsi_id {
  141.     int host_no;        /* as in "scsi<n>" where 'n' is one of 0, 1, 2 etc */
  142.     int channel;
  143.     int scsi_id;        /* scsi id of target device */
  144.     int lun;
  145.     int scsi_type;      /* TYPE_... defined in scsi/scsi.h */
  146.     int unused1;        /* probably find a good use, set 0 for now */
  147.     int unused2;        /* ditto */
  148.     int unused3;  
  149. } Sg_scsi_id;
  150.  
  151. /* ioctls  ( _GET_s yield result via 'int *' 3rd argument unless 
  152.             otherwise indicated */
  153. #define SG_SET_TIMEOUT 0x2201  /* unit: jiffies, 10ms on i386 */
  154. #define SG_GET_TIMEOUT 0x2202  /* yield timeout as _return_ value */
  155.  
  156. #define SG_EMULATED_HOST 0x2203 /* true for emulated host adapter (ATAPI) */
  157.  
  158. /* Used to configure SCSI command transformation layer for ATAPI devices */
  159. #define SG_SET_TRANSFORM 0x2204
  160. #define SG_GET_TRANSFORM 0x2205
  161.  
  162. #define SG_SET_RESERVED_SIZE 0x2275  /* currently ignored, future addition */
  163. /* Following yields buffer reserved by open(): 0 <= x <= SG_SCATTER_SZ */
  164. #define SG_GET_RESERVED_SIZE 0x2272
  165.  
  166. /* The following ioctl takes a 'Sg_scsi_id *' object as its 3rd argument. */
  167. #define SG_GET_SCSI_ID 0x2276   /* Yields fd's bus,chan,dev,lun+type */
  168. /* SCSI id information can also be obtained from SCSI_IOCTL_GET_IDLUN */
  169.  
  170. /* Override adapter setting and always DMA using low memory ( <16MB on i386).
  171.    Default is 0 (off - use adapter setting) */
  172. #define SG_SET_FORCE_LOW_DMA 0x2279  /* 0-> use adapter setting, 1-> force */
  173. #define SG_GET_LOW_DMA 0x227a   /* 0-> use all ram for dma; 1-> low dma ram */
  174.  
  175. /* When SG_SET_FORCE_PACK_ID set to 1, pack_id is input to read() which
  176.    will attempt to read that pack_id or block (or return EAGAIN). If 
  177.    pack_id is -1 then read oldest waiting. When ...FORCE_PACK_ID set to 0
  178.    (default) then pack_id ignored by read() and oldest readable fetched. */ 
  179. #define SG_SET_FORCE_PACK_ID 0x227b
  180. #define SG_GET_PACK_ID 0x227c /* Yields oldest readable pack_id (or -1) */
  181.  
  182. #define SG_GET_NUM_WAITING 0x227d /* Number of commands awaiting read() */
  183.  
  184. /* Turn on error sense trace (1..8), dump this device to log/console (9)
  185.    or dump all sg device states ( >9 ) to log/console */
  186. #define SG_SET_DEBUG 0x227e    /* 0 -> turn off debug */
  187.  
  188. /* Yields max scatter gather tablesize allowed by current host adapter */
  189. #define SG_GET_SG_TABLESIZE 0x227F  /* 0 implies can't do scatter gather */
  190.  
  191. /* Control whether sequencing per file descriptor (default) or per device */
  192. #define SG_GET_MERGE_FD 0x2274   /* 0-> per fd (default), 1-> per device */
  193. #define SG_SET_MERGE_FD 0x2273   /* Attempt to change sequencing state,
  194.   if more than 1 fd open on device, will fail with EBUSY */
  195.  
  196. /* Get/set command queuing state per fd (default is SG_DEF_COMMAND_Q) */
  197. #define SG_GET_COMMAND_Q 0x2270   /* Yields 0 (queuing off) or 1 (on) */
  198. #define SG_SET_COMMAND_Q 0x2271   /* Change queuing state with 0 or 1 */
  199.  
  200.  
  201. #define SG_DEFAULT_TIMEOUT (60*HZ) /* HZ == 'jiffies in 1 second' */
  202. #define SG_DEFAULT_RETRIES 1
  203.  
  204. /* Default modes, commented if they differ from original sg driver */
  205. #define SG_DEF_COMMAND_Q 0
  206. #define SG_DEF_MERGE_FD 0       /* was 1 -> per device sequencing */
  207. #define SG_DEF_FORCE_LOW_DMA 0  /* was 1 -> memory below 16MB on i386 */
  208. #define SG_DEF_FORCE_PACK_ID 0
  209.  
  210. /* maximum outstanding requests, write() yields EDOM if exceeded */
  211. #define SG_MAX_QUEUE 16
  212.  
  213. #define SG_SCATTER_SZ (8 * 4096)  /* PAGE_SIZE not available to user */
  214. /* Largest size (in bytes) a single scatter-gather list element can have.
  215.    The value must be a power of 2 and <= (PAGE_SIZE * 32) [131072 bytes on 
  216.    i386]. The minimum value is PAGE_SIZE. If scatter-gather not supported
  217.    by adapter then this value is the largest data block that can be
  218.    read/written by a single scsi command. Max number of scatter-gather
  219.    list elements seems to be limited to 255. */
  220.  
  221. #define SG_BIG_BUFF SG_SCATTER_SZ       /* for backward compatibility */
  222. /* #define SG_BIG_BUFF (SG_SCATTER_SZ * 8) */ /* =256KB, if you want */
  223.  
  224. #endif
  225.